package com; /**
 * @author Xue ShuWen
 * @date 2023/6/9 17:20
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public  class UserDaoImpl {
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String    url = "jdbc:mysql://localhost:3306/stuInfo";
    static String   root = "root";
    static String     ps = "root";

    Connection coon = null;
    Statement st = null;
    ResultSet rs = null;


    public String queryUser(String uname,String upassword) throws ClassNotFoundException {
        Class.forName(driver);
        String sql = "SELECT * FROM userInfo where uname='"+uname+"'&& upassword="+"'"+upassword+"';";

        try {
            coon = DriverManager.getConnection(url,root,ps);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            st = coon.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()){
                return "true";
//                System.out.println("用户名是"+rs.getString("uname"));
            }else{
                System.out.println("用户账号不存在或者密码错误");
                return "false";
            }


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[]args) throws ClassNotFoundException {
        Scanner input = new Scanner(System.in);

        System.out.println("请输入账号:");
        String uname = input.next();

        System.out.println("请输入密码:");
        String upassword = input.next();

        new UserDaoImpl().queryUser(uname,upassword);

    }
}












